/******************************************************************************/
/* mainwindow.ccp : zzg Version 1.3                                           */
/* A C++/Qt4 program : to plan scheduled computer shut down                   */
/* Ptyxs (Cassian Braconnier) - july 2010                                     */
/* Lic. GPLv3                                                                 */
/* Provided with no guaranty whatsoever.                                      */
/******************************************************************************/

//------------------------------------------------------------------------------

#include <QtGui>

#include "mainwindow.h"
#include "zzg.h"

//------------------------------------------------------------------------------

MainWindow::MainWindow()
{
    setWindowTitle(tr("Fin de partie"));

    zzg = new Zzg;
    setCentralWidget(zzg);

    createActions();
    createMenus();

    disconnect(zzg->closeButton, SIGNAL(clicked(bool)), zzg, SLOT(close()));
    connect(zzg->closeButton, SIGNAL(clicked(bool)), this, SLOT(close()));

    readSettings();
}

//------------------------------------------------------------------------------

void MainWindow::createActions()
{
    soundAction = new QAction(tr("Avertissement &sonore"), this);
        soundAction->setCheckable(true);
        soundAction->setChecked(false);
        connect(soundAction, SIGNAL(toggled(bool)), zzg, SLOT(setNoisyWarning(bool)));
}

//------------------------------------------------------------------------------

void MainWindow::createMenus()
{
    optionsMenu = menuBar()->addMenu(tr("&Options"));
    optionsMenu->addAction(soundAction);
}

//------------------------------------------------------------------------------

// pour demander confirmation avant fermeture
// dans le cas où un arrêt a été planifié
void MainWindow::closeEvent(QCloseEvent *event)
{
    if (zzg->timer->isActive()) {

        int r = QMessageBox::warning(this, tr("Zzg"),
tr("Un arrêt de l'ordinateur est planifié, souhaitez-vous vraiment l'annuler et quitter Zzg ?"),
QMessageBox::Yes | QMessageBox::No);

        if (r == QMessageBox::Yes) {
            writeSettings();
            event->accept();
        }
        else if (r == QMessageBox::No)
            event->ignore();

    }
    else
        writeSettings();
}

//------------------------------------------------------------------------------

void MainWindow::writeSettings()
{
    QSettings settings("Cassian von Ptyxs", "Zzg");

    settings.setValue("noisyWarning", soundAction->isChecked());
}

//------------------------------------------------------------------------------

void MainWindow::readSettings()
{
    QSettings settings("Cassian von Ptyxs", "Zzg");

    bool noisyWarning = settings.value("noisyWarning", false).toBool();
    soundAction->setChecked(noisyWarning);
}

//------------------------------------------------------------------------------


// FIN MAINWINDOW.CPP
